﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRQIDT.functions
{
    public partial class Simulation : UserControl
    {
        public Simulation()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool Termination_flag = false;
            double Exe_time = 0.0;//模拟运行时间
            double Fault_time = 0.0;//故障发生的时间
            double Fault_time_sum = 0.0;//失效时间总和
            double A0 = 0.0;//可用度
            double T0 = 0.0, T1 = 0.0;//T0为任务计划总时间，T1为故障回复总时间
            int Failure_count = 0;//任务失败次数
            double Failure_time = 0.0;//任务失败时间
            Random RD;
            double RandomNumber;
            int count=int.Parse(this.textBox1.Text);//仿真次数
            //开始测试
            for (int n = 0; n < count; n++)
            {
                //任务段循环
                for (int i = 0; (i < Form1.TS_count) && (Form1.TSA[i].TS_isdeleted == false) && (Form1.TSA[i].TS_isselected == true); i++)
                {
                    if (Termination_flag == true)//判断是否出现致命故障，任务终止
                        break;
                    Exe_time = Form1.TSA[i].Excuse_Time;//任务段的实际执行时间
                    T0 += Form1.TSA[i].Excuse_Time;
                    //失效模式循环
                    for (int j = 0; (j < Form1.TSA[i].FailureModeCount) && (Form1.FMA[i].FM_isdeleted == false); j++)
                    {
                        if (Termination_flag == true)//判断是否出现致命故障，任务终止
                            break;
                        //随机失效时间
                        RD = new Random();
                        RandomNumber = RD.NextDouble();
                        Fault_time += -(Math.Log(RandomNumber) / Form1.FMA[i].FailureRate);
                        while (Fault_time <= Exe_time)//有失效，添加入失效故障记录
                        {
                            if (Termination_flag == true)//判断是否出现致命故障，任务终止
                                break;
                            //添加入失效故障记录
                            Form1.FR_count++;
                            Form1.FRA[Form1.FR_count - 1] = new Form1.FailureRecord();
                            Form1.FRA[Form1.FR_count - 1].TS_Name = Form1.TSA[i].TS_Name;
                            Form1.FRA[Form1.FR_count - 1].FM_id = Form1.FMA[j].FM_id;
                            Form1.FRA[Form1.FR_count - 1].FM_lv = Form1.FMA[j].Level;
                            Form1.FRA[Form1.FR_count - 1].FMH_time = Fault_time;
                            Form1.FRA[Form1.FR_count - 1].FMR_time = Fault_time + Form1.FMA[j].Recovery_time; 
                            switch(Form1.FRA[Form1.FR_count - 1].FM_lv)
                            {
                                case 1://失效模式1，有恢复时间
                                    T1 += Fault_time;
                                    Exe_time += Fault_time;
                                    break;
                                case 2://失效模式2，无恢复时间
                                    break;
                                case 3://失效模式3，任务终止
                                    Failure_count++;
                                    Failure_time += Fault_time;
                                    Termination_flag = true;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }  
                Fault_time_sum += Form1.FRA[Form1.FR_count - 1].FMH_time;//失效时间总和
                A0 += T0 / (T0 + T1);//A的累加

            }
            //计算参数
            double MTBF = Fault_time_sum / Form1.FR_count ;//MTBF
            A0 = A0 / count;//A可用度
            double MCSP = (count - Failure_count) / count;//MCSP任务成功概率
            double MTBCF = Failure_time / Failure_count;//MTBCF
            
            //输出结果
            this.textBox2.Text = MTBF.ToString();
            this.textBox3.Text = A0.ToString();
            this.textBox4.Text = MCSP.ToString();
            this.textBox5.Text = MTBCF.ToString();
        }
    }
}
